package com.example.qing.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.qing.config.AuthAccess;
import com.example.qing.config.LogType;
import com.example.qing.config.MyLogs;
import com.example.qing.pojo.Result;
import com.example.qing.pojo.Novel;
import com.example.qing.pojo.User;
import com.example.qing.service.NovelService;
import com.example.qing.utils.JwtUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/novel")
public class NovelController {

    @Resource
    NovelService novelService;

    @MyLogs(operation = "文章",type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        novelService.removeById(id);
        return Result.success();
    }



    @MyLogs(operation = "文章",type = LogType.BATCH_DELETE)
    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<Integer> ids){
        log.info("批量删除");
        novelService.removeByIds(ids);

        return Result.success();
    }

    @MyLogs(operation = "文章",type = LogType.ADD)
    @PostMapping("/add")
    public Result save(@RequestBody Novel novel){
        novel.setTime(LocalDateTime.now());
        novel.setUpdateTime(LocalDateTime.now());
        novelService.save(novel);
        return Result.success();
    }
    @GetMapping("/total")
    public Result total(){
        long count= novelService.count();
        return Result.success(count);
    }


/*
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){

        Novel novel=novelService.getById(id);
        return Result.success(novel);
    }
*/

    @MyLogs(operation = "文章",type = LogType.UPDATE)
    @PutMapping("/update")
    public Result update(@RequestBody Novel novel){
        novel.setUpdateTime(LocalDateTime.now());
        novelService.updateById(novel);
        return Result.success();
    }



    @AuthAccess
    @GetMapping("/selectByPage")
    public Result adminList(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize,
                            @RequestParam String type,
                            @RequestParam String title,
                            @RequestParam Integer permission){
        log.info("分页查询");
        QueryWrapper<Novel>queryWrapper=new QueryWrapper<Novel>();
        queryWrapper.like(StrUtil.isNotBlank(type),"type",type);
        queryWrapper.like(StrUtil.isNotBlank(title),"name",title);
        if (permission!=2) {
            queryWrapper.like("permission",permission);
        }
        queryWrapper.orderByDesc("id");
        Page<Novel>page=novelService.page(new Page<>(pageNum,pageSize),queryWrapper);
        return Result.success(page);
    }

    @AuthAccess
    @GetMapping("/select/recommend")
    public Result list(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize){
        log.info("分页查询");
        QueryWrapper<Novel>queryWrapper=new QueryWrapper<Novel>();
        queryWrapper.eq("recommend",true);
        queryWrapper.orderByDesc("id");
        Page<Novel>page=novelService.page(new Page<>(pageNum,pageSize),queryWrapper);
        return Result.success(page);
    }

}
